const app = getApp()
const utils = require('../../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    capBarMoveFlag:false,
    filterList:[
      {
        id:1,
        flag:true,
        name:"综合"
      },
      {
        id:2,
        flag:false,
        name:"最多点赞"
      },
      {
        id:3,
        flag:false,
        name:"最多浏览"
      }
    ],
    typeOption: [
      { text: '方案名称', value: 0 },
      { text: '设计师', value: 1 }
    ],
    orderType:1,
    searchTypeVlue:0,
    searchValue:"",
    navHeight: app.globalData.navHeight,
    boundButtonRect:{   },
    headUrl:"",
    height:'',
    id:"",
    specialTypeId:'',
    cardList:'', // 专题列表
    associationList:'', // 专题相关联的列表
    page: 1,
    isNoMoreData: false
  },
  handleSelectFilter(e){
    const item=e.currentTarget.dataset.list
   const newFilter= this.data.filterList.map(f=>{
      if(f.id===item.id){
        f.flag=true
      }else{
        f.flag=false
      }
      return f
    })
    this.setData({
      filterList:newFilter,
      orderType:item.id
    })
    this.getProgrammes()
  },
  onChange(e){
    this.setData({
      searchValue:e.detail
    })
  },
  onSearch(e){
    this.setData({
      searchValue:e.detail
    })
    this.getProgrammes()
  },
  onSearchs(e){
    this.getProgrammes()
  },
  menuChange(e){
    this.setData({
      searchTypeVlue:e.detail
    })
  },
  //获取主题图片
  GetgetSpecial(){
    let id = this.data.id
    utils.$http(app.globalData.api.GetgetSpecial + id,{},'POST',true,'JSON').then(res=>{
      if(res.success === 1 && res.data) {
        this.setData({
          headUrl: res.data.appCoverImage
        })
      }
    })
  },
  //获取相同类型下所用专题
  GetgetSpecialtype(){
    let id = this.data.id
    utils.$http(app.globalData.api.GetgetSpecialtype + id,{},'GET',true,'JSON').then(res=>{
      if(res.data){
        let associationList = res.data
        this.setData({
          associationList
        })
      }
    })
  },
  //获取方案列表
  getProgrammes(page, type){
    let specialId = this.data.id
    const { orderType,searchTypeVlue,searchValue} =this.data
    const str={
      onlyCountFlag: false,
      page: page || 1,
      pageFlag: true,
      rows: 10,
      specialId,
      startRow: 0,
      orderType
      // totalFlag: true
    }
    if(searchValue&&searchValue.length>0){
      if(searchTypeVlue===0){
      str.programmeName=searchValue
    }else if(searchTypeVlue===1){
      str.userName=searchValue
    }
    }
    utils.$http(app.globalData.api.GetgetProgrammes,str,'POST',true,'JSON').then(res=> {
      if(res.success === 1 && res.data) {
        const { records = [] } = res.data
        if(type === 'more') {
          if(records.length === 0) {
            this.setData({
              isNoMoreData: true
            })
          } else {
            this.setData({
              cardList: this.data.cardList.concat(records),
              isNoMoreData: false
            })
          }
        } else {
            this.setData({
              cardList: records,
              isNoMoreData: false
            })
          }
        }
    })
  },
  getBoundButtonRect:function(){
    let data = wx.getMenuButtonBoundingClientRect()
    const screenWidth =  app.globalData.screenWidth 
    let left = screenWidth - data.right
    let top = data.top
    let height = data.height
    let width = data.width
    this.setData(
      {
         boundButtonRect: {
           left,
           height,
           top,
           width
         }
      }
    )
  },
  setupInit:function(){
    utils.getFrameById('#navigationBar', (frame) => {
      // console.log('frame:', frame)
      if (frame && frame.height) {
        this.setData({
          navHeight: frame.height
        })
        // console.log('frame:', frame)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBoundButtonRect()
    this.setupInit()
    // this.GetSpecialList()
    let data = options.id
    data = JSON.parse(data)
    let {specialTypeId = '',id = ''} = data
    // 定义导航栏的高度   方便对齐
    this.setData({
      height: app.globalData.height,
      id,
      specialTypeId,
    })
    this.getProgrammes()
    this.GetgetSpecial()
    this.GetgetSpecialtype()
  },
  // 返回上一页
  goBack:function(){
    const currentPages = getCurrentPages()
    if (currentPages.length <= 1){
       wx.switchTab({
         url: '/pages/home/main/index',
       })
    }  else {
      wx.navigateBack({
        delta: 1,
        success: function (e) {
          const page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
          page.onLoad();
         }
      })
    }
  },
  // 返回首页
  goHome:function(){
    wx.switchTab({
      url: '/pages/home/main/index'
    })
  },
  onPageScroll(e){
    const isSatisfy = e.scrollTop  >= 100  // 高度大于100的时候 显示白底标签栏
    if(this.data.capBarMoveFlag == isSatisfy) { 
      return false;
    } else {
      this.setData({
        capBarMoveFlag: isSatisfy
      })
    }
  },
  goSpecialTopicPage(e){ 
    let id = JSON.stringify(e.currentTarget.dataset.id) 
    wx.navigateTo({ 
      url: `/pages/home/homePage/SpecialTopic/index?id=${id}`, 
    }) 
  }, 
  goDetailsPage(e){ 
    let id = e.currentTarget.id 
    wx.navigateTo({ 
      url: `/pages/Details/main/index?id=${id}`, 
    }) 
  } , 
  //跳转全屋
  goPanoramaUrl(e){
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: `/pages/H5/main/index?url=${encodeURIComponent(url)}`,
    })
  },
   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getProgrammes()
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    },2000)
 },

 /**
  * 页面上拉触底事件的处理函数
  */
 onReachBottom: function () {
    this.data.page = this.data.page + 1
    this.getProgrammes(this.data.page, 'more')
 },
})

